// pages/userdata/userdata.js
import {
    updateUser,
    editLogo
} from '../../api/user'
import {
    getUserInfo
} from '../../api/article'
const FormData = require('../../utils/formData')
Page({

    /**
     * 页面的初始数据
     */
    data: {
        show: false,
        parmas: {
            headUrl:'',
            nick: '',
            sex:null
        },
        user:[]
    },

    /**
     * 生命周期函数--监听页面加载
     */
    async onLoad(options) {
        const res = await getUserInfo({targetUserId:wx.getStorageSync('user').userId})
        if(!res.status){
            this.setData({
                'parmas.headUrl':res.content.headUrl,
                'parmas.nick':res.content.nick,
                'parmas.sex':res.content.sex,
            })
        }
    },
    onChange(e) {
        this.setData({
            'parmas.nick': e.detail
        })
    },
    onChange2(e) {
        this.setData({
            'parmas.sex': e.detail,
            show: false,
        })
    },
    onClose() {
        this.setData({
            show: false
        })
    },
    handleOpen() {
        this.setData({
            show: true
        })
    },
    onChooseAvatar(e){
        const {
            avatarUrl
        } = e.detail
        let params = new FormData()
        const _this = this
        wx.uploadFile({
            url: 'https://jianxiangjinjiren.com/user/web/uploadImg',
            filePath:avatarUrl,
            name: 'imgFile',
            header: {
                'content-type': params.getData().contentType,
                'token': wx.getStorageSync('token')
            },
            formData: {
                'type': 7,
                'userId': wx.getStorageSync('user').userId,
            },
            success(res) {
                if (!JSON.parse(res.data).status) {
                    wx.showToast({
                        title: '上传成功',
                        icon: 'success'
                    })
                    wx.hideLoading()
                    _this.setData({
                        'parmas.headUrl': JSON.parse(res.data).content.imageURL
                    })
                    let user = wx.getStorageSync('user')
                    user.headUrl = JSON.parse(res.data).content.imageURL
                    wx.setStorage({
                        key: 'user',
                        data: user
                    })
                }
            }
        });
        this.setData({
            'parmas.headUrl':avatarUrl
        })
    },
    async handleEdit() {
        wx.getUserProfile({
          desc: 'desc',
          success:(res)=>{
              console.log(res,'xxx')
          }
        })
        const res = await updateUser(this.data.parmas)
        if (!res) {
            let user = wx.getStorageSync('user')
            user.nick = this.data.parmas.nick
            wx.setStorage({
                key: 'user',
                data: user
            })
            wx.navigateBack()
        }
    },
    handleImg() {
        const _this = this
        wx.chooseMedia({
            count: 1,
            mediaType: ['image'],
            sizeType: ['original', 'compressed'],
            success:  (res) => {
                wx.showLoading({
                    title: '上传中...',
                })
                
                let params = new FormData()
                wx.uploadFile({
                    url: 'https://jianxiangjinjiren.com/user/web/uploadImg',
                    filePath: res.tempFiles[0].tempFilePath,
                    name: 'imgFile',
                    header: {
                        'content-type': params.getData().contentType,
                        'token': wx.getStorageSync('token')
                    },
                    formData: {
                        'type': 7,
                        'userId': wx.getStorageSync('user').userId,
                    },
                    success(res) {
                        if (!JSON.parse(res.data).status) {
                            wx.showToast({
                                title: '上传成功',
                                icon: 'success'
                            })
                            wx.hideLoading()
                            _this.setData({
                                'parmas.headUrl': JSON.parse(res.data).content.imageURL
                            })
                            let user = wx.getStorageSync('user')
                            user.headUrl = JSON.parse(res.data).content.imageURL
                            wx.setStorage({
                                key: 'user',
                                data: user
                            })
                        }
                    }
                });
            },
            fail: err => {
                wx.hideLoading()
                console.log(err)
            }
        })
    },
    /**
     * 生命周期函数--监听页面初次渲染完成
     */
    onReady() {

    },

    /**
     * 生命周期函数--监听页面显示
     */
    onShow() {

    },

    /**
     * 生命周期函数--监听页面隐藏
     */
    onHide() {

    },

    /**
     * 生命周期函数--监听页面卸载
     */
    onUnload() {

    },

    /**
     * 页面相关事件处理函数--监听用户下拉动作
     */
    onPullDownRefresh() {

    },

    /**
     * 页面上拉触底事件的处理函数
     */
    onReachBottom() {

    },

    /**
     * 用户点击右上角分享
     */
    onShareAppMessage() {

    }
})